package redisClient

import (
	"gitee.com/lv_baobao/gcore"
	"gitee.com/lv_baobao/gcore/logHelper"
	"github.com/go-redis/redis"
	"time"
)

type IRedisClient interface {
	init()
	Get(key string, objPtr interface{}) error
	Incr(key string) error
	Add(key string, value interface{}, duration time.Duration) error
	Del(key string) error
	Expire(key string, duration time.Duration) error
	LPush(key string, value interface{}) (error, int64)
	LRange(key string, start int64, stop int64) ([]string, error)
	LIndex(key string, index int64) (string, error)
	LSet(key string, index int64, value interface{}) error
	LRem(key string, count int64, value interface{}) error
	LTrim(key string, start int64, stop int64) error
	HSet(key string, field string, value interface{}) error
	HGet(key string, field string, objPtr interface{}) error
	Pipeline() redis.Pipeliner
}

type RedisClient struct {
	Client IRedisClient
}

func NewRedisClient() IRedisClient {
	redisConfig := gcore.NewAppSettingsHelper().GetAppConfig().Redis
	if redisConfig.ServerType == gcore.NormalClient {
		return NewNormalClient()
	} else if redisConfig.ServerType == gcore.FailoverClient {
		return NewFailoverClient()
	} else if redisConfig.ServerType == gcore.Cluster {
		return NewClusterClient()
	} else {
		logHelper.Error("初始化RedisClient失败，ServerType配置错误")
	}
	return nil
}

func (r *RedisClient) Get(key string, objPtr interface{}) error {
	return r.Client.Get(key, objPtr)
}

func (r *RedisClient) Add(key string, value interface{}, duration time.Duration) error {
	return r.Client.Add(key, value, duration)
}

func (r *RedisClient) Del(key string) error {
	return r.Client.Del(key)
}

func (r *RedisClient) Expire(key string, duration time.Duration) error {
	return r.Client.Expire(key, duration)
}

func (r *RedisClient) LPush(key string, value interface{}) (error, int64) {
	return r.Client.LPush(key, value)
}

func (r *RedisClient) LRange(key string, start int64, stop int64) ([]string, error) {
	return r.Client.LRange(key, start, stop)
}

func (r *RedisClient) LIndex(key string, index int64) (string, error) {
	return r.Client.LIndex(key, index)
}

func (r *RedisClient) LSet(key string, index int64, value interface{}) error {
	return r.Client.LSet(key, index, value)
}

func (r *RedisClient) LRem(key string, count int64, value interface{}) error {
	return r.Client.LRem(key, count, value)
}

func (r *RedisClient) LTrim(key string, start int64, stop int64) error {
	return r.Client.LTrim(key, start, stop)
}

func (r *RedisClient) HSet(key string, field string, value interface{}) error {
	return r.Client.HSet(key, field, value)
}

func (r *RedisClient) HGet(key string, field string, objPtr interface{}) error {
	return r.Client.HGet(key, field, objPtr)
}

func (r *RedisClient) Pipeline() redis.Pipeliner {
	return r.Client.Pipeline()
}
